package edu.towson.cosc.cosc455.biedermann.compiler;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class MyCompiler {
	
	public static String token;
	static MyLexicalAnalyzer lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer generator;
	static String inputTxt = "";
	static ArrayList<String> parsedInput;
	
	
	/**
	 * Checks for one argument
	 * @param args
	 * @throws FileNotFoundException
	 */
	/**
	 * Checks for valid file ending
	 * @param args
	 * @throws FileNotFoundException
	 */
	/**
	 *Reads in input from file string by string
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException{
		
		token = "";
		
		//Check that there is 1 arg passed
		if(args.length != 1){
			System.err.println("ERROR! : More than one argument found when one is expected");
			System.exit(0);
		}
		
		if(!args[0].substring((args[0].length()-4), args[0].length()).equals(".lol")){
			System.err.println("ERROR! : Invalid file ending when .lol is expected");
			System.exit(0);
		}
		
		parsedInput = new ArrayList<String>();
		
		File file = new File(args[0]);
		Scanner in = new Scanner(file);
		while(in.hasNext()){
			inputTxt += in.next() + " ";
		}
		in.close();
		
		LexicalTokens intialize = new LexicalTokens();
		intialize.addToMap();
		
		
		lexer = new MyLexicalAnalyzer();
		lexer.getNextToken();
		
		parser = new MySyntaxAnalyzer();
		parser.lolcode();
		
		generator = new MySemanticAnalyzer();
		generator.translate();
		
		//writes final translation to an output file that will generate and html file
		FileWriter writer = new FileWriter(args[0].substring(0, args[0].length()-4)+".html");
		for(String str: LexicalTokens.finalText) {
		  writer.write(str);
		}
		writer.close();
		
		
	}
}

